from fastapi import FastAPI
from starlette.responses import StreamingResponse
from langserve import add_routes
import os

from app import chatbot,chatbot2

app = FastAPI()

@app.get("/chat")
def chat(message: str):
    def generate():
        for chunk in chatbot():
            if chunk.content:
                # 使用 SSE 格式返回数据
                yield f"{chunk.content}"
        yield "数据已完成1"

        for chunk in chatbot2():
            if chunk.content:
                # 使用 SSE 格式返回数据
                yield f"{chunk.content}"
        yield "数据已完成2"


    return StreamingResponse(
        generate(),
        media_type="text/event-stream"
    )

if __name__ == "__main__":
    import uvicorn
    
    server = dict(
        port=int(os.getenv('FLASK_PORT', 8088)),
        debug=os.getenv('FLASK_DEBUG', 'False').lower() in ['true', '1', 't']
    )
    uvicorn.run("api:app", host="0.0.0.0", port=server['port'])
